package com.bxshey.happyTown.controller;



import com.bxshey.happyTown.dto.user.UpdateDto;
import com.bxshey.happyTown.entity.User;
import com.bxshey.happyTown.result.Result;
import com.bxshey.happyTown.service.UserService;
import com.bxshey.happyTown.utils.JwtUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * (User)表控制层
 *
 * @author makejava
 * @since 2024-01-03 22:42:38
 */
@RestController
@RequestMapping("/user")
public class UserController{

    /**
     * 服务对象
     */
    @Resource
    private UserService userService;


    /**
     * 通过openId查询单条数据
     *
     * @param openid openid
     * @return 单个用户信息
     */
    @GetMapping("{openid}")
    public Result<?> selectOne(@PathVariable String openid) {
        return Result.success(userService.queryOpenID(openid));
    }



    /**
     * 修改数据
     *
     * @param user 实体对象
     * @return 成功个数
     */
    @PutMapping
    public Result<?> update(@RequestHeader("Authorization_token") String token,@RequestBody UpdateDto user) {
        User info = JwtUtils.checkToken(token);
        if (Objects.isNull(info)) {
            return Result.error();
        }
        user.setId(info.getId());
        return Result.success(userService.update(user));
    }


    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public Result<?> delete(@RequestParam("idList") List<Long> idList) {
        return Result.success("");
    }


    /**
     * 修改数据
     *
     * @param file 上传文件
     * @return 图片路径
     */
    @PostMapping("/avatar")
    public Result<?> updateAvatar(@RequestBody MultipartFile file) {
        return Result.success(userService.updateAvatar(file));
    }



}

